let wxToastLock = false;
let wxLoadingLock = false;
export function timeoutTask(task, time) {
    let tmpTimeout = setTimeout(() => {
        task();
        clearTimeout(tmpTimeout);
    }, time)
}
export function isNumber(val) {

    const regPos = /^\d+(\.\d+)?$/; //非负浮点数

    const regNeg = /^(-(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*)))$/; //负浮点数

    let flag = false;

    if (regPos.test(val) || regNeg.test(val)) {

        flag = true;

    }
    return flag;
}
// 获取随机值  min<= num < max
export const getRandom = (min, max) => Math.floor(Math.random() * (max - min)) + min;

export const showWxToast = (pTip) => {
    if (!wxToastLock && !wxLoadingLock) {
        wxToastLock = true;
        wx.showToast({
            'title': pTip,
            'icon': 'none'
        });
        timeoutTask(() => {
            hideWxToast();
        }, 1500);
    } else if (!wxToastLock && wxLoadingLock) {
        hideWxLoading();
        showWxToast(pTip);
    } else if (wxToastLock && !wxLoadingLock) {
        hideWxToast();
        showWxToast(pTip);
    } else {
        console.log('两个锁都是锁住状态 showWxToast');
    }
}

export const showWxLoading = (pTip) => {
    if (!wxToastLock && !wxLoadingLock) {
        wxLoadingLock = true;
        wx.showLoading({
            title: pTip,
            mask: true,
        });
    } else if (!wxToastLock && wxLoadingLock) {
        hideWxLoading();
        showWxLoading(pTip);
    } else if (wxToastLock && !wxLoadingLock) {
        hideWxToast();
        showWxLoading(pTip);
    } else {
        console.log('两个锁都是锁住状态 showWxLoading');
    }
}

export const hideWxLoading = () => {
    if (wxLoadingLock) {
        wx.hideLoading();
        wxLoadingLock = false;
    } else {
        console.log('特殊情况hideWxLoading');
    }
}
const hideWxToast = () => {
    if (wxToastLock) {
        wx.hideToast();
        wxToastLock = false;
    } else {
        console.log('特殊情况hideWxToast');
    }
}

// 生成随机id(随机字母加当前时间戳构成)
export const generateRandomId = (pLen) => {
  let tmpId = '';
  const charStr = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz';
  const tmpLen1 = charStr.length;
  const tmplen2 = pLen || 6; // 生成的id字母部分的长度，默认长度为6
  for (let i = 0; i < tmplen2; i += 1) {
    tmpId += charStr.charAt(Math.floor(Math.random() * tmpLen1));
  }
  tmpId += new Date().getTime();
  return tmpId;
};

export const timeIncrement = (endTime) => {
  const date = new Date();
  const intervals = endTime - date.getTime();

  const a = intervals % 1000 / 10;
  const b = intervals % 60000 / 1000;
  const c = intervals % 3600000 / 60000;
  return {
    total: intervals,
    ms: (a < 10) ? (`0${Math.floor(a)}`) : (Math.floor(a)),
    s: (b < 10) ? (`0${Math.floor(b)}`) : (Math.floor(b)),
    m: (c < 10) ? (`0${Math.floor(c)}`) : (Math.floor(c)),
  };
};

// 支持小时数的倒计时函数
export const timeIncrementWithHours = (endTime) => {
  const date = new Date();
  const intervals = endTime - date.getTime();

  const a = intervals % 1000 / 10;
  const b = intervals % 60000 / 1000;
  const c = intervals % 3600000 / 60000;
  const d = intervals / 3600000;
  
  return {
    total: intervals,
    ms: (a < 10) ? (`0${Math.floor(a)}`) : (Math.floor(a)),
    s: (b < 10) ? (`0${Math.floor(b)}`) : (Math.floor(b)),
    m: (c < 10) ? (`0${Math.floor(c)}`) : (Math.floor(c)),
    h: (d < 10) ? (`0${Math.floor(d)}`) : (Math.floor(d)),
  };
};